import { ref, computed, onMounted, onUnmounted, watchEffect } from 'vue'
import { useKeyboardStore } from '@/stores'

export interface UseKeyboardOptions {
    // 是否自动启动监听
    autoStart?: boolean
    // 是否在组件卸载时自动停止监听
    autoStop?: boolean
    // 自定义样式配置
    styleConfig?: {
        type?: 'margin' | 'padding' | 'height'
        direction?: 'bottom' | 'top'
    }
    // 键盘状态变化回调
    onChange?: (state: { height: number, isVisible: boolean }) => void
}

/**
 * 键盘状态管理组合式函数
 *
 * 使用示例：
 * ```ts
 * const {
 *   keyboardHeight,
 *   isKeyboardVisible,
 *   adaptiveStyle,
 *   startListening,
 *   stopListening
 * } = useKeyboard({
 *   autoStart: true,
 *   onChange: (state) => console.log('Keyboard state:', state)
 * })
 * ```
 */
export function useKeyboard(options: UseKeyboardOptions = {}) {
    const {
        autoStart = false,
        autoStop = true,
        styleConfig = { type: 'margin', direction: 'bottom' },
        onChange
    } = options

    const keyboardStore = useKeyboardStore()

    // 响应式状态
    const keyboardHeight = computed(() => keyboardStore.height)
    const isKeyboardVisible = computed(() => keyboardStore.isVisible)
    const isListening = computed(() => keyboardStore.isListening)
    const keyboardInfo = computed(() => keyboardStore.keyboardInfo)

    // 自适应样式
    const adaptiveStyle = keyboardStore.getAdaptiveStyle(styleConfig)

    // 启动监听
    const startListening = () => {
        return keyboardStore.startListening()
    }

    // 停止监听
    const stopListening = () => {
        return keyboardStore.stopListening()
    }

    // 重置状态
    const resetState = () => {
        keyboardStore.resetState()
    }

    // 获取统计信息
    const getStatistics = () => {
        return keyboardStore.getStatistics()
    }

    // 监听键盘状态变化
    if (onChange) {
        watchEffect(() => {
            onChange({
                height: keyboardHeight.value,
                isVisible: isKeyboardVisible.value
            })
        })
    }

    // 生命周期管理
    onMounted(() => {
        if (autoStart) {
            startListening()
        }
    })

    onUnmounted(() => {
        if (autoStop && isListening.value) {
            stopListening()
        }
    })

    return {
        // 状态
        keyboardHeight,
        isKeyboardVisible,
        isListening,
        keyboardInfo,
        adaptiveStyle,

        // 方法
        startListening,
        stopListening,
        resetState,
        getStatistics,

        // Store 实例（用于高级用法）
        keyboardStore
    }
}

/**
 * 简化版键盘监听 Hook
 * 只提供基本的键盘状态，自动管理生命周期
 */
export function useSimpleKeyboard() {
    return useKeyboard({
        autoStart: true,
        autoStop: true
    })
}

/**
 * 键盘适配样式 Hook
 * 专门用于获取键盘自适应样式
 */
export function useKeyboardStyle(
    type: 'margin' | 'padding' | 'height' = 'margin',
    direction: 'bottom' | 'top' = 'bottom'
) {
    const keyboardStore = useKeyboardStore()
    return keyboardStore.getAdaptiveStyle({ type, direction })
}

/**
 * 键盘状态监听 Hook
 * 提供键盘状态变化的监听能力
 */
export function useKeyboardWatcher(
    callback: (state: { height: number, isVisible: boolean }) => void
) {
    const keyboardStore = useKeyboardStore()

    watchEffect(() => {
        callback({
            height: keyboardStore.height,
            isVisible: keyboardStore.isVisible
        })
    })

    return {
        height: computed(() => keyboardStore.height),
        isVisible: computed(() => keyboardStore.isVisible)
    }
}

export default useKeyboard